package com.manshutingyu.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.manshutingyu.common.core.dto.LoginUserDTO;
import com.manshutingyu.common.core.dto.R;
import com.manshutingyu.system.domain.SysUser;
import com.manshutingyu.system.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author：yinyuxia
 * @Date：2025/4/23 15:41
 */
@RestController
@RequestMapping("/user")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    @GetMapping(value = "/page")
    public R<IPage<SysUser>> page(@ParameterObject Page<SysUser> page, @ParameterObject SysUser sysUser) {
        return R.success(sysUserService.getPage(page, sysUser));
    }


    @GetMapping(value = "/{id}")
    public R<SysUser> getById(@PathVariable("id") Long id) {
        return R.success(sysUserService.getUserById(id));
    }


    @PostMapping
    public R<Integer> save(@RequestBody SysUser sysUser) {
        return sysUserService.createUser(sysUser) ? R.success() : R.error();
    }


    @PutMapping
    public R<Integer> update(@RequestBody SysUser sysUser) {
        return sysUserService.updateUser(sysUser) ? R.success() : R.error();
    }


    @DeleteMapping(value = "/{ids}")
    public R<Integer> deleteByIds(@Parameter(description = "多个以英文逗号(,)拼接") @PathVariable Long[] ids) {

        return sysUserService.deleteByIds(ids) ? R.success() : R.error();
    }


    @PutMapping(value = "/resetPwd/{id}")
    public R<Integer> resetPwd(@PathVariable Long id, String password) {

        return sysUserService.updatePasswordByUserId(id, password) ? R.success() : R.error();
    }


    @GetMapping("/info/{username}")
    public R<LoginUserDTO> selectByUsername(@PathVariable("username") String username) {
        return sysUserService.selectByUsername(username);
    }
}
